// pages/reply/index.js
import http from '../../utils/api' // 引入api接口管理文件
import common from '../../utils/util' // 引入api接口管理文件

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploaderList: [],
    uploaderNum:0,
    image:[],
    showUpload:true,
    video : '',
    video_url : '',
    content:'', // 默认无内容
  },

  // 提交数据
  btn_fabiao:function(){
    // 提交动态数据
    let that = this.data;
    let image = that.image.join('|');  // 图片数组处理
    let param = {comment_id:that.id,content:that.content,image:image,video:that.video_url};
    http.news_comment_answer({  // 请求添加接口
      data:param,
      success:res=>{
        app.tip(res.info);
        if(res.code == 1){
          setTimeout(function () {
            //要延时执行的代码
            wx.navigateTo({
              url: '../reply_all/index?id='+that.id,
            })
          }, 1500) //延迟时间
        }
      },
      fail:err=>{
        console.log(err)
      }
    })
  },


  // 删除图片
  clearImg:function(e){
    var nowList = [];//新数据
    var uploaderList = this.data.uploaderList;//原数据
    var nowImage = [];
    var image = this.data.image;
    for (let i = 0; i < uploaderList.length;i++){
        if (i == e.currentTarget.dataset.index){
            continue;
        }else{
            nowList.push(uploaderList[i])
            nowImage.push(image[i]);
        }
    }
    this.setData({
        uploaderNum: this.data.uploaderNum - 1,
        uploaderList: nowList,
        image:nowImage,
        showUpload: true
    })
},
//展示图片
showImg:function(e){
    var that=this;
    wx.previewImage({
        urls: that.data.uploaderList,
        current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
},
upload:function(){
  var that=this
  let uploaderList = that.data.uploaderList;
  if(uploaderList.length==0){
    wx.showActionSheet({
      itemList: ['视频','图片'],
      success (res) {
        if(res.tapIndex==0){
          that.chooseVideo()
        }else{
          that.chooseImage()
        }
      }
    })
  }else{
    that.chooseImage()
  }
},
//上传图片
chooseImage() {
    var that = this;
    wx.chooseImage({
        count: 9 - that.data.uploaderNum, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
            wx.showLoading({title: '正在上传...'}) // 开始上传显示遮罩层
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFilePaths;
            let uploaderList = that.data.uploaderList.concat(tempFilePaths);
            // 上传到服务器
            let imageArr = [];
            for(let i=0;i<uploaderList.length;i++){
              common.upload(uploaderList[i],function(res){
                if(res.statusCode==200){
                  imageArr.push(JSON.parse(res.data).url);
                  that.setData({
                    image:imageArr
                 })
                }else{
                  app.tip(res.errMsg);return false;
                }
              })
            }
           wx.hideLoading() // 上传成功关闭遮罩层
           if (uploaderList.length==9){
            that.setData({
                showUpload:false
            })
        }
        if(uploaderList.length > 9){
          app.tip('最多上传九张图片')
        }
            that.setData({
                uploaderList: uploaderList,
                uploaderNum: uploaderList.length,
            })
        }
    })
},

// 上传视频
chooseVideo() {
  let that = this;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      compressed: true,
      maxDuration: 10,
      camera: 'back',
      success: res => {

        wx.showLoading({title: '正在上传...'}) // 开始上传显示遮罩层
        const video = res.tempFilePath;
        // 上传视频到服务器
        common.upload(video,function(res){
          if(res.statusCode==200){
            let video_url = JSON.parse(res.data).url;
            that.setData({
              video_url :video_url
            })
            wx.hideLoading() // 上传成功关闭遮罩层
          }else{
            app.tip(res.errMsg);return false;
          }
        })
        this.setData({
          video:video,
          showUpload:false
        })
      }
    })
},
// 删除视频
videoDelete(){
  wx.showModal({
    title: '警告',
    content: '确定要删除该视频吗',
    success: res => {
      if (res.confirm) {
        this.setData({
          video: '',
          showUpload:true,
          video_url:'',
        })
      }
    }
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let id = options.id;
      let content = options.content;
      let that = this;
      that.setData({
        content_title:content,
        id:id,
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取内容
  getContent:function(e){
    this.setData({
      content:e.detail.value
    })  
  },

})